<?php
namespace app\common\model;

use think\Db;
use think\Model;

class Company extends Model
{
    // 指定表名,不含前缀
    protected $name = 'company';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    /**
     * 获得公司列表
     */
    public function getList($admin_reg,$projectid=0){
        $condition = '';
        if ($projectid!=0)$condition = " AND project_id=".$projectid;
       
		
		
		$parent_id = Db::name('plaform')->where(['admin_reg'=>$_SESSION["think"]["reg"]])->field('parentid')->find();
		if($parent_id['parentid']){

			$adreg = Db::name('plaform')->where(['id'=>$parent_id['parentid'],'status'=>1])->field('admin_reg')->find();
			$rg = empty($adreg['admin_reg']) ? [$admin_reg] : [$admin_reg,$adreg['admin_reg']];
			
			
			$arrc = ['status'=>1,'admin_reg'=>['in',$rg],'isdelete'=>0];
			if ($projectid!=0) $arrc['project_id'] = $projectid;
			
			$arr=$this->field('id,name,pid')->where($arrc)->select();
			
		} else {
			  $arr=$this->field('id,name,pid')->where('status=1 and admin_reg= "'.$admin_reg.'" and isdelete=0 '.$condition)->select();
			
		}
		
        return $arr;
    }

    public function getListByProjectId($id,$admin_reg){
        $arr=$this->field('id,name,pid')->where('status=1 and admin_reg= "'.$admin_reg.'" and project_id='.$id)->select();
        return $arr;
    }

    /**
     * 根据公司名 判断公司是否存在 不存在则添加公司
     */
    public function getComanyIsExistByCompanyName($CompanyName,$project_id){
        $admin_reg = $_SESSION['think']['reg'];
        if (!isset($CompanyName))return false;
        $Id = $this->field('id,name')->where([is_numeric($CompanyName)?'id':'name'=>$CompanyName,'admin_reg'=>$admin_reg,'status'=>1])->find();
        if (is_null($Id)){
            $data = [
                'name'=>$CompanyName,
                'pid'=>0,
                'remark'=>'',
                'create_time'=>time(),
                'update_time'=>time(),
                'isdelete'=>0,
                'status'=>1,
                'project_id'=>$project_id,
                'admin_reg'=>$admin_reg,
            ];

            $id = $this->insert($data,false,true);
            if ($id){
                return ['name'=>$CompanyName,'id'=>$id];
            }else{
                return false;
            }
        }else{
            return ['name'=>$Id['name'],'id'=>$Id['id']];
        }

    }
    /**
     * 获取公司列表
     * @param string admin_reg
     * @return array 公司列表数组
     */
    public function getCompanyList($admin_reg){
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);
        $where = ['admin_reg'=>['in',$child_plaform]];
        $result = Db::name($this->name)->field('id,name,pid')->where($where)->where(' status=1 ')->select();
        return $result;
    }

   /**
     * 公司是否存在
     * @param string strName 公司名称
     * @param string admin_reg 平台身份
     * @return array 公司列表数组
     */

   public function companyExist($strName,$adminReg,$id=0){

    if(empty($strName)) return false;
    $condition = array('name' => $strName,'admin_reg'=>$adminReg);
    if(!empty($id))  $condition['id'] = array('<>', $id);   
    return Db::name($this->name)->where($condition)->find();
   }
}

